package com.production.CoursePlatform.controller;

import com.production.CoursePlatform.entity.LearnReplay;
import com.production.CoursePlatform.service.LearnReplayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/learnReplays")
public class LearnReplayController {

    @Autowired
    private LearnReplayService learnReplayService;

    // 创建新的学习回复
    @PostMapping("/create")
    public ResponseEntity<String> createLearnReplay(@RequestBody LearnReplay learnReplay) {
        learnReplayService.createLearnReplay(learnReplay);
        return ResponseEntity.ok("LearnReplay created successfully!");
    }

    // 根据ct_id查找学习回复
    @GetMapping("getByCtId/{ct_id}")
    public ResponseEntity<List<LearnReplay>> getLearnReplayByCtId(@PathVariable int ct_id) {
        List<LearnReplay> learnReplays = learnReplayService.findLearnReplayByCtId(ct_id);
        if (learnReplays != null) {
            return ResponseEntity.ok(learnReplays);
        } else {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
        }
    }

    // 根据lr_id查找学习回复
    @GetMapping("getByLrId/{lr_id}")
    public ResponseEntity<LearnReplay> getLearnReplayByLrId(@PathVariable int lr_id) {
        LearnReplay learnReplay = learnReplayService.findLearnReplayByLrId(lr_id);
        if (learnReplay != null) {
            return ResponseEntity.ok(learnReplay);
        } else {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
        }
    }

    // 删除指定lr_id的学习回复
    @DeleteMapping("delete/{lr_id}")
    public ResponseEntity<String> deleteLearnReplay(@PathVariable int lr_id) {
        boolean isDeleted = learnReplayService.deleteLearnReplay(lr_id);
        if (isDeleted) {
            return ResponseEntity.ok("LearnReplay deleted successfully!");
        } else {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body("LearnReplay not found");
        }
    }

    // 更新学习回复
    @PutMapping("/update")
    public ResponseEntity<String> updateLearnReplay(@RequestBody LearnReplay learnReplay) {
        boolean isUpdated = learnReplayService.updateLearnReplay(learnReplay);
        if (isUpdated) {
            return ResponseEntity.ok("LearnReplay updated successfully!");
        } else {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body("LearnReplay not found");
        }
    }
}

